﻿using System.Threading.Tasks;
using KWT.DRG_DIP.DO.System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

namespace KWT.DRG_DIP.API.Infrastructure
{
    // You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
    [Authorize]
    public class TenantInfoMiddleware
    {
        private readonly RequestDelegate _next;
        public TenantInfoMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            var tenantInfo = context.RequestServices.GetRequiredService<TenantInfo>();
            var tenant_ID = context.Request.Headers["TenantID"];

            if (!string.IsNullOrEmpty(tenant_ID))
            {
                tenantInfo.Name = tenant_ID;
            }
            else
            {
                tenantInfo.Name = "main";
            }
            await _next(context);
        }
    }
}
